package {
	import camo.core.decal.DecalSheet;
	import camo.core.events.LoaderManagerEvent;
	import camo.core.managers.CamoPropertySheetManager;
	import camo.core.managers.DecalSheetManager;
	
	import com.adobe.serialization.json.JSON;
	
	import components.DecalDisplay;
	import components.InternalPreloader;
	import components.Label;
	import components.Preloader;
	
	import connect.MyConnectionImpl;
	
	import container.WindowSelectAuto;
	import container.WindowWheelParameters;
	
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	import managers.GlobalDecalSheetManager;
	import managers.GlobalPropertySheetManager;
	
	[SWF( width="980", height="700", backgroundColor="#3A3E4A", framerate="24" )]
	
	public class FittingRoom extends Sprite
	{
		private var propSheet: 					CamoPropertySheetManager;
		public static var pass:					String = "shinaprof.pass";
		public static var url:					String;
		public static var internalPreloader:	InternalPreloader;
		private var preloaderText:				TextField;
		private var currentLoaded:				int;
		private var numberLoader:				int;
		private var arrayBrend:					Array;
		private var windowSelectAuto:			DecalDisplay;
		private var windowWheelParameters:		DecalDisplay;
		private var shadowing:					DecalDisplay;
		
		public static const DecalNameOver:String = "Over";
		public static const DecalNameDown:String = "Down";
		
		public function FittingRoom(){
			var urlLoaderSettings:URLLoader = new URLLoader();  
			urlLoaderSettings.addEventListener(Event.COMPLETE, settingsLoaded);  
			urlLoaderSettings.load(new URLRequest("xml/settings.xml"));
			
			function settingsLoaded(e:Event):void {
				e.target.removeEventListener(Event.COMPLETE, settingsLoaded);
				url = XML(e.target.data).url;
				preloaderText = new TextField();
				preloaderText.autoSize = TextFieldAutoSize.LEFT;
				preloaderText.selectable = false;
				preloaderText.x = XML(e.target.data).preloaderText.attributes()[0];
				preloaderText.y = XML(e.target.data).preloaderText.attributes()[1];
				preloaderText.textColor = XML(e.target.data).preloaderText.attributes()[2];
				preloaderText.text = XML(e.target.data).preloaderText+" настройки";
				addChild(preloaderText);
				internalPreloader = new InternalPreloader(XML(e.target.data).preloader.attributes()[0],
					XML(e.target.data).preloader.attributes()[1], XML(e.target.data).preloader.attributes()[2], XML(e.target.data).preloader);
				internalPreloader.addEventListener("completePreloader", loadIndicator);
				
				function loadIndicator(e:Event):void {
					e.target.removeEventListener("completePreloader", loadIndicator);
					preloaderText.appendText(", индикатор");
					addChild(internalPreloader);
					internalPreloader.start();
					loadAsync();
					
					function loadAsync():void {
						currentLoaded = 0;
						numberLoader = 6;
						//numberLoader = 5;
						var fontLoader: Loader = new Loader();
						fontLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, fontLoaded);  
						fontLoader.load(new URLRequest("swfs/fonts/FontLibrary.swf"));
						
						var cssLoader : URLLoader = new URLLoader( );
						propSheet = GlobalPropertySheetManager.instance;  
						cssLoader.addEventListener(Event.COMPLETE, cssLoaded);  
						cssLoader.load( new URLRequest("css/main.properties.css"));
						
						var decalLoader:URLLoader = new URLLoader();  
						decalLoader.addEventListener(Event.COMPLETE, loadDecal, false, 0, true);  
						decalLoader.load(new URLRequest("xml/decalsheet.xml"));
						
						var xmlrpcParameters:Array = new Array();
						xmlrpcParameters[0] = ["method","cat_data"];
						xmlrpcParameters[1] = ["tid","auto"];
						xmlrpcParameters[2] = ["subject","brend"];
						var myConnectionImpl:MyConnectionImpl = new MyConnectionImpl(url, pass, xmlrpcParameters)
						myConnectionImpl.addEventListener("completeXmlRpc", loadBrend);
						
						function fontLoaded(e:Event):void {
							e.target.removeEventListener(Event.COMPLETE, fontLoaded);
							preloaderText.appendText(", шрифты");
							init();
						}
						function cssLoaded(e:Event):void {
							e.target.removeEventListener(Event.COMPLETE, cssLoaded);
							preloaderText.appendText(", таблица стилей");
							var target : URLLoader = e.target as URLLoader;  
							propSheet.parseCSS("global.properties", target.data);
							
							shadowing = new DecalDisplay("Box-SelectAuto_Shadowing");
							if (propSheet.getSelector("#Box-SelectAuto_Shadowing").toString().search("backgroundImage:url") != -1){
								shadowing.addEventListener("BGImageLoad", loadShadowing);
							} else {init()}
							addChildAt(shadowing, numChildren - 2);
							
							windowSelectAuto = new DecalDisplay("Box-SelectAuto_Window");
							if (propSheet.getSelector("#Box-SelectAuto_Window").toString().search("backgroundImage:url") != -1){
								windowSelectAuto.addEventListener("BGImageLoad", loadSelectAutoWindow);
							} else {init()}
							
							windowWheelParameters = new DecalDisplay("Box-WheelParameters_Window");
							if (propSheet.getSelector("#Box-WheelParameters_Window").toString().search("backgroundImage:url") != -1){
								windowWheelParameters.addEventListener("BGImageLoad", loadWheelParametersWindow);
							} else {init()}
							
							function loadShadowing(e:Event):void {
								e.target.removeEventListener("BGImageLoad", loadShadowing);
								preloaderText.appendText(", фоновое затенение");
								init();
							}
							function loadSelectAutoWindow(e:Event):void {
								e.target.removeEventListener("BGImageLoad", loadSelectAutoWindow);
								preloaderText.appendText(", окно Авто");
								init();
							}
							function loadWheelParametersWindow(e:Event):void {
								e.target.removeEventListener("BGImageLoad", loadWheelParametersWindow);
								preloaderText.appendText(", окно Параметров");
								init();
							}
						}
						function loadDecal(e:Event):void {
							e.target.removeEventListener(Event.COMPLETE, loadDecal);
							preloaderText.appendText(", таблица картинок");
							var xml:XML = XML(e.target.data);  
							var decalSheetManager:DecalSheetManager = GlobalDecalSheetManager.instance;  
							decalSheetManager.addEventListener(LoaderManagerEvent.PRELOAD_NEXT, onPreloadNext, false, 0, true);  
							decalSheetManager.addEventListener(LoaderManagerEvent.PRELOAD_DONE, onDecalSheetsLoad, false, 0, true);  
							decalSheetManager.parseXML(xml);
							
							function onPreloadNext(e:LoaderManagerEvent):void{
								preloaderText.appendText(", скин "+e.data.totalPreloaded+"/"+e.data.totalPreloading);
							}
							function onDecalSheetsLoad(e:LoaderManagerEvent):void{
								e.target.removeEventListener(LoaderManagerEvent.PRELOAD_DONE, onDecalSheetsLoad);
								e.target.removeEventListener(LoaderManagerEvent.PRELOAD_NEXT, onPreloadNext);
								
								var decalWindow: Array = new Array();
								decalWindow[0] = ["ButtonWindowSelectAuto", "ButtonWindowWheelParameters"];
								decalWindow[1] = ["ButtonOverWindowSelectAuto", "ButtonOverWindowWheelParameters"];
								decalWindow[2] = ["ButtonDownWindowSelectAuto", "ButtonDownWindowWheelParameters"];
								
								for (var i:int = 0; i < decalWindow[0].length; i++){
									var buttonsOver: DecalSheet = e.target.getSheet(decalWindow[0][i]);
									for (var key:Object in buttonsOver.decalRectangles){
										GlobalDecalSheetManager.instance.newDecal(key+DecalNameOver,decalWindow[1][i],
											buttonsOver.decalRectangles[key].x,	buttonsOver.decalRectangles[key].y,
											buttonsOver.decalRectangles[key].width,buttonsOver.decalRectangles[key].height);
										GlobalDecalSheetManager.instance.newDecal(key+DecalNameDown,decalWindow[2][i],
											buttonsOver.decalRectangles[key].x,	buttonsOver.decalRectangles[key].y,
											buttonsOver.decalRectangles[key].width,buttonsOver.decalRectangles[key].height);
									}
								}
								//trace(GlobalDecalSheetManager.instance.getSheet("ButtonOverWindowSelectAuto").decalNames);
								init();
							}
						}
						function loadBrend(e:Event):void {
							e.target.removeEventListener("completeXmlRpc", loadBrend);
							preloaderText.appendText(", бренды");
							var jsonTemp:Array = JSON.decode(e.target.data)
							arrayBrend = new Array();
							arrayBrend.length = jsonTemp[0].resp.length;
							for (var i:int; i < arrayBrend.length; i++){
								if (jsonTemp[0].resp[i].brend_id == 0) arrayBrend.length = arrayBrend.length-1;
								else arrayBrend[i] = [jsonTemp[0].resp[i].brend_id, jsonTemp[0].resp[i].brend_brend];
							}
							init();
						}
						function init():void{
							currentLoaded++;
							if (currentLoaded == numberLoader) {
								
								removeChild(preloaderText);
								
								var windowSelectAuto: WindowSelectAuto = new WindowSelectAuto(windowSelectAuto, arrayBrend);
								addChildAt(windowSelectAuto, numChildren - 1);
								//windowSelectAuto.visible = false;
								internalPreloader.stop();
								
								//var windowWheelParameters: WindowWheelParameters = new WindowWheelParameters(windowWheelParameters);
								//addChild(windowWheelParameters);
							}
						}
					}
				}
			}
		}
	}
}